package converter;

import java.io.*;

import java.util.ArrayList;

import java.util.List;

/**
 * Created by Сергей on 09.09.2014.
 */
public class PointConverterImp implements PointConverter {

    // method gets to read the file and returns a list of Points
    @Override
    public List<Point> convertFromFile( File pointsFile ) throws IOException {

        FileInputStream fileInputStream = null;

        BufferedReader bufferedReader = null;

        String line;

        int i = 0;


        // read the file
        try {

            fileInputStream = new FileInputStream( pointsFile );

        } catch ( FileNotFoundException e ) {

            e.printStackTrace();

        }


        // read into the buffer the incoming stream
        try {

            bufferedReader = new BufferedReader( new InputStreamReader( fileInputStream,"CP1251" ));

        } catch ( UnsupportedEncodingException e ) {

            e.printStackTrace();

        }


        // create a list of points
        List<Point> pointsList = new ArrayList<Point>();

        while ( ( line = bufferedReader.readLine() ) !=null ){

            // separation title from a list of points
            if (i < 4){

                i++;

            }

            else {

                String p[] = line.split( "," );

                PointImplementation pointimp = new PointImplementation();

                pointimp.setPoint( p );

                pointsList.add( pointimp );

            }
        }

        bufferedReader.close();
        fileInputStream.close();

        return pointsList;
    }
}
